﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

using RemoteScriptingLibrary;
using Win32 = RemoteScriptingLibrary.Microsoft.Win32.Win32;


namespace WindowsRemoteScripting.Action
{
    public class WindowFinder : AbstractCommand
    {
        Timer m_Timer = new Timer();

        public WindowFinder()
        {
            m_Timer.Tick += new EventHandler(m_Timer_Tick);
        }

        void m_Timer_Tick(object sender, EventArgs e)
        {
            WindowInfo info = RetrieveCurrentWindowInfo();
            string sText = info.Caption;
                
            Form1.m_Form.tbCaption.Text = sText;
            Form1.m_Form.tbClassName.Text = info.ClassName;
            Form1.m_Form.tbModule.Text = info.ModuleFilename;
            Form1.m_Form.tbHandle.Text = info.Handle.ToString();
            Form1.m_Form.tbText.Text = info.Text;
        }

        public void Stop()
        {
            m_Timer.Stop();
        }

        public static WindowInfo RetrieveCurrentWindowInfo()
        {
            Win32.POINT ptPOINT = Point.Empty;
            Win32.GetCursorPos(out ptPOINT);
            IntPtr ptr = Win32.WindowFromPoint(ptPOINT);
            WindowInfo info = new WindowInfo();
            info.RetrieveInfo(ptr);
            return info;
        }

        public override void Run(object sender, EventArgs e)
        {
            if (Form1.m_Form.chkbWindowFinder.Checked)
            {
                m_Timer.Start();
            }
            else
            {
                m_Timer.Stop();
            }
        }
    }
}

